package me.youline.dataServer.service;

import java.util.Collections;
import java.util.List;

import javax.transaction.Transactional;

import me.youline.dataServer.dao.SocialLatestNotificationRecordDAO;
import me.youline.dataServer.dao.socialActionMsg.SocialActionFollowingMsgDAO;
import me.youline.dataServer.dao.socialActionMsg.SocialActionMsgNotifyRecordDAO;
import me.youline.dataServer.dao.socialActionMsg.SocialActionPraiseMsgDAO;
import me.youline.dataServer.entity.socialActionMsg.SocialActionBaseMsg;
import me.youline.dataServer.entity.socialActionMsg.SocialActionMsgNotifyRecord;
import me.youline.dataServer.entity.socialActionMsg.SocialActionMsgNotifyRecord.NotifyMsgType;
import me.youline.dataServer.utils.IdPagination;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 社交行为通知消息业务类
 * @author linhan
 *
 */
@Service
public class SocialActionNotifyMsgService {

	@Autowired
	private SocialLatestNotificationRecordDAO latestDAO;

	@Autowired
	private SocialActionMsgNotifyRecordDAO notifyRecordDAO;

	@Autowired
	private SocialActionPraiseMsgDAO praiseMsgDAO;

	@Autowired
	private SocialActionFollowingMsgDAO followingMsgDAO;

	/**
	 * 获取指定用户的通知动态列表数据
	 * @param userId
	 * @param page
	 * @return
	 */
	public List<SocialActionMsgNotifyRecord> getSocialActionMsgsByUserId(
			Long userId, IdPagination page) {
		return notifyRecordDAO.getSocialActionMsgNotifyRecords(userId, page);
	}

	/**
	 * 根据指定动态消息id列表及动态类型查询动态消息数据
	 * @param msgIds
	 * @param msgType
	 * @return
	 */
	public List<? extends SocialActionBaseMsg> getSocialActionMsgs(List<Long> msgIds , NotifyMsgType msgType) {
		if(NotifyMsgType.FOLLOWING == msgType){
			return followingMsgDAO.getSocailNotifications(msgIds);
		}else if(NotifyMsgType.PRAISE == msgType){
			return praiseMsgDAO.getSocailNotifications(msgIds);
		}
		return Collections.emptyList();
	}

	/**
	 * 根据关注人ID及被关注人ID，删除此次关注行为产生的 关注动态通知
	 * @param userId
	 * @param followingId
	 */
	@Transactional
	public void deleteFollowingNotifyMsg(Long userId, Long followingId) {
		List<Long> msgIds = followingMsgDAO.getMsgIdsByUserAndFollowing(userId,
				followingId);
		notifyRecordDAO.deleteNoticeRecord(msgIds, NotifyMsgType.FOLLOWING);
		followingMsgDAO.batchDeleteSocialActionMsgById(msgIds);
	}

	/**
	 * 根据操作人ID及原创公告ID，删除此次点赞行为产生的 动态通知
	 * @param userId
	 * @param originId
	 */
	@Transactional
	public void deletePraiseNotifyMsg(Long userId, Long originId) {
		List<Long> msgIds = praiseMsgDAO.getMsgIdsByInitiatorAndOriginId(
				userId, originId);
		notifyRecordDAO.deleteNoticeRecord(msgIds, NotifyMsgType.PRAISE);
		praiseMsgDAO.batchDeleteSocialActionMsgById(msgIds);
	}

	/**
	 * 根据原创公告id删除与此相关的点赞动态与转发动态
	 * @param originId
	 */
	@Transactional
	public void deleteNotifyMsgByOrigin(Long originId) {
		List<Long> praiseMsgIds = praiseMsgDAO.getMsgIdsByTargetId(originId);
		notifyRecordDAO.deleteNoticeRecord(praiseMsgIds, NotifyMsgType.PRAISE);
		praiseMsgDAO.batchDeleteSocialActionMsgById(praiseMsgIds);
	}

}
